package org.salatec.tapestry4e.propertytester;

import org.eclipse.core.expressions.PropertyTester;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.salatec.tapestry4e.TMLPlugin;
import org.salatec.tapestry4e.util.EditorUtils;
import org.salatec.tapestry4e.util.Logger;
import org.salatec.tapestry4e.util.T5ComponentsStore;

public class ComponentPropertyTester extends PropertyTester {

	private static final String IS_COMPONENT = "isComponent";

	public ComponentPropertyTester() {
		super();

	}

	public boolean test(Object receiver, String property, Object[] args,
			Object expectedValue) {
		IWorkbenchPage activePage = PlatformUI.getWorkbench()
				.getActiveWorkbenchWindow().getActivePage();

		if (activePage.getSelection() instanceof ITextSelection
				&& IS_COMPONENT.equalsIgnoreCase(property)) {
			IFile file = EditorUtils.findActiveFile();
			String path = file.getProjectRelativePath().toString();
			Logger.log(Logger.INFO, "currentFile: " + path);
			T5ComponentsStore store = TMLPlugin.getDefault().getStore(
					file.getProject());
			for (String pkg : store.getApplicationPackages()) {
				pkg = pkg.replace('.', '/');
				if (path.indexOf(pkg) >= 0) {
					return true;
				}
			}

		}
		return false;
	}
}
